package jun.empdepmanagersystem02.aop;

import jun.empdepmanagersystem02.mapper.OperateLogMapper;
import jun.empdepmanagersystem02.pojo.OperateLog;
import jun.empdepmanagersystem02.pojo.Result;
import jun.empdepmanagersystem02.until.JWTuntils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;

@Component
@Aspect

public class AspectFunction {
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private OperateLogMapper operateLogMapper;

    @Around("@annotation(jun.empdepmanagersystem02.aop.LogFuntion)")
    public Object logFunction(ProceedingJoinPoint pjp) throws Throwable {
        Result tokenResult = JWTuntils.verify(request.getHeader("token"));

        Object result = null;
        if (tokenResult.getCode() == 1) {
            String oprateUser = (String) tokenResult.getData();
            System.out.println(oprateUser);
            LocalDateTime oprateTime = LocalDateTime.now();
            String className = pjp.getTarget().getClass().getName();
            String methodName = pjp.getSignature().getName();
            String methodParams = Arrays.toString(pjp.getArgs());
            long begin = System.currentTimeMillis();
            result = pjp.proceed();
            Result result1=(Result) result;

            long end = System.currentTimeMillis();
            String returnvalue = "";
            if (result != null) {
                returnvalue = result1.getData().toString();
            }
            operateLogMapper.insert(new OperateLog(null, oprateUser, oprateTime, className, methodName, methodParams, returnvalue, end - begin));
        }
        return result;
    }

}
